package com.gdkm.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.exceptions.TemplateInputException;

import com.gdkm.po.Page;
import com.gdkm.po.Student;
import com.gdkm.po.User;
import com.gdkm.service.UserService;

@Controller
@RequestMapping(value = "/ssm", method = RequestMethod.GET)
public class UserController {
	@Autowired
	UserService service;

	@RequestMapping("/register")
	public String register() {
		return "register";
	}

	@RequestMapping("/person")
	public String person() {
		return "person";
	}

	@GetMapping("/uregister")
	public String save(String user_code, String user_name, String user_password, String user_state,
			HttpServletRequest request) {

		List<User> list = service.gettest(user_code);
		System.out.println(list);
		service.save(user_code, user_name, user_password, user_state);
		return "login";
		
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String login() {
		return "login";

	}

	@RequestMapping(value = "/ulogin", method = RequestMethod.POST)

	public String denglu(User user, HttpSession session) {

		String user_code = user.getUser_code();
		String user_password = user.getUser_password();

		try {
			User sqluser = service.get(user_code);
			System.out.println(sqluser.getUser_code());
			String sqlpassword = sqluser.getUser_password();
			String user_name = sqluser.getUser_name();
			session.setAttribute("USER_SESSION", user);
			if (user_code != null && user_password != null && user_password.equals(sqlpassword)) {
				return "index";
			}

		} catch (NullPointerException e) {
			System.out.println("账号不存在");
		}
		return "login";
	}

	@RequestMapping("/save")
	public String save(String usercode, String password, String name, String age, String grade, String phoneNumber,
			String sex, String type) {

		service.save1(usercode, password, name, age, grade, phoneNumber, sex, type);
		return "index";

	}

	@RequestMapping("/index")
	public String index() {
		return "index";
	}

	@ResponseBody
	@RequestMapping("/list")
	public List get(String name, String grade, String type, int y) {

		List<Student> list = service.list(name, grade, type, (y - 1) * 8, 8);
		System.out.println(list);
		return list;
	}

	//
	@ResponseBody
	@RequestMapping("/list1")
	public List get(int y) {
		List<Student> list1 = service.list1((y - 1) * 8, 8);
		return list1;
	}

	@RequestMapping("/index1")
	public String index1() {
		return "index1";
	}

	@RequestMapping("/del")
	public String del(int id) {
		service.delete1(id);
		return "index";
	}

	// 更新
	@RequestMapping("/update")
	public String update(Student stu) {
		service.update(stu);
		System.out.print(stu);
		return "index";
	}
}